using System;
using System.Collections.Generic;
using System.Linq;

namespace Framework.DataObjects
{
    public static class XmlParameterExtensions_NameTextValueDo
    {
        /// <summary>
        /// Set the named parameter with a value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters">The parameters.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public static void Set<T>(this List<NameTextValueDo> parameters, string name, T value)
        {
            Set(parameters, -1, name, value);
        }

        /// <summary>
        /// Set the named parameter with a value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters">The parameters.</param>
        /// <param name="insertIndex">Index of the insert.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public static void Set<T>(this List<NameTextValueDo> parameters, int insertIndex, string name, T value)
        {
            var param = Get(parameters, name);

            var storeValue = Is.IsNull(value) ? string.Empty : value.ToString();

            if (param == null)
            {
                param = new NameTextValueDo(name, storeValue);

                if (insertIndex == -1 || insertIndex > parameters.Count - 1)
                {
                    parameters.Add(param);
                }
                else
                {
                    parameters.Insert(insertIndex, param);
                }
            }
            else
            {
                param.Value = storeValue;
            }
        }

        /// <summary>
        /// Get the parameter as a value, defaults to String.Empty if not found.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetValue(this List<NameTextValueDo> parameters, string name)
        {
            return GetValue(parameters, name, String.Empty);
        }

        /// <summary>
        /// Get the parameter as a value, uses a default value if not found.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="name">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static string GetValue(this List<NameTextValueDo> parameters, string name, string defaultValue)
        {
            var param = Get(parameters, name);

            return (param == null ? defaultValue : StrUtil.DefaultValue(param.Value, defaultValue));
        }

        /// <summary>
        /// Gets the parameter.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static NameTextValueDo Get(this List<NameTextValueDo> parameters, string name)
        {
            return parameters.FirstOrDefault(parameter => Is.EqualString(parameter.Name, name));
        }

        /// <summary>
        /// Converst the list of Name/Values to dictionary of strings
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        public static IDictionary<string, string> ToDictionary(this List<NameTextValueDo> parameters)
        {
            return parameters.ToDictionary(parameter => parameter.Name, parameter => parameter.Value);
        }
    }
}